<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            >
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage"> 添加商品</el-button>
        </el-col>
      </el-row>

      <!--    表格区域 -->
      <el-table :data="GoodsList" style="width: 100%" :stripe="true" border>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="680">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建日期" width="180px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.goods_id)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="removeUserById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>

      <!-- 修改提示框 -->
    </el-card>

    <!--修改区域 -->
    <el-dialog title="提示" :visible.sync="showDialogVisible" width="60%">
      <!-- 表单区 -->
      <el-form
        :model="showEditId"
        :rules="showEditIdRules"
        ref="showEditIdRef"
        label-width="100px"
      >
         <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="showEditId.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="showEditId.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="showEditId.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model="showEditId.goods_weight"></el-input>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="pushEditDialog"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品列表数据 参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      GoodsList: [],
      total: 0,
      // 控制修改的开关
      showDialogVisible: false,
      // 获取id的内容
      showEditId: [
      ],
      idInfo:'',
      showEditIdRules:{
          goods_name: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur',
          },
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            trigger: 'blur',
          },
        ],
        goods_number: [
          {
            required: true,
            message: '请输入商品数量',
            trigger: 'blur',
          },
        ],
        goods_weight: [
          {
            required: true,
            message: '请输入商品重量',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      // 发起get
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }
      this.$message.success('获取成功')
      this.GoodsList = res.data.goods
      this.total = res.data.total
    },
    // 底部切换页 显示条数页面  每页条数发生变化就会触发此函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 当前页发生变化就会触发此函数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },

    // 修改
    async showEditDialog(id) {
      // 根据id查询商品
      this.idInfo = id;
      const { data: res } = await this.$http.get('goods/' + id)
      console.log(res.data)

      if (res.meta.status !== 200) {
        return this.$message.error('查询失败')
      }
      this.$message.success('查询成功')
      // 把查询到的数据存到showEditId
      this.showEditId = res.data
      // 打开对话框
      this.showDialogVisible = true
    },
    // 提交修改
    pushEditDialog(){
       this.$refs.showEditIdRef.validate(async valid=>{
        if(!valid){return this.$message.error("请输入正确信息")}

        // 发起put请求
       const {data:res} = await this.$http.put('goods/'+this.idInfo,this.showEditId)
        console.log(res.data)
        if(res.meta.status !==200){
          return this.$message.error('修改失败')
        }
        this.$message.success('修改成功')
        this.showDialogVisible = false
        this.getGoodsList()
      })
    },
    // 删除
    async removeUserById(id) {
      const confirmRestult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      console.log(confirmRestult)
      if (!confirmRestult == 'confirm') {
        return
      }

      // 已经确认删除
      // 发起delete请求
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getGoodsList()
    },
    // 添加商品的页面
    goAddpage() {
      this.$router.push('/goods/add')
    },
  },
}
</script>

<style lang="less" scoped></style>
